package com.appsl.manager;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;

import com.appsl.objects.Lugar;
import com.google.android.gms.maps.model.Marker;

public class DataManager 
{
	public static List<Lugar> readFavourites(Activity act)
	{
		List<Lugar> lstFavs = new ArrayList<Lugar>();
		
		InputStream is;
		try 
		{
			is = act.getAssets().open("favs/favoritos.txt");

			BufferedReader br = new BufferedReader(new InputStreamReader(is));

			String nombre = br.readLine();
			
			while (nombre != null)
			{
				String cX = br.readLine();
				String cY = br.readLine();
				lstFavs.add(new Lugar(0, nombre, cX, cY, ""));
				
				nombre = br.readLine();
			}
		} 
		catch (Exception ex) 
		{
			AlertManager.showInfoMessage(act, ex.getMessage());
		}
		
		return lstFavs;
	}

	public static void writeFavourites(Activity act, ArrayList<?> lstMarker) 
	{
		try 
		{
			FileOutputStream fos = act.openFileOutput("favs/favoritos.txt", Context.MODE_PRIVATE);
			
			for (Object obj : lstMarker)
			{
				if (obj.getClass() == Marker.class)
				{
					Marker mk = (Marker)obj;
					String temp = mk.getTitle() + "\n"; 
					fos.write(temp.getBytes());
					
					temp = mk.getPosition().latitude + "\n";
					fos.write(temp.getBytes());
					
					temp = mk.getPosition().longitude+ "\n";
					fos.write(temp.getBytes());
				}
			}
			
			fos.flush();
			fos.close();
		} 
		catch (Exception ex) 
		{
			AlertManager.showInfoMessage(act, ex.getMessage());
		}
	}
}
